﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Microsoft.Win32;

namespace GUI.src {
    public class TrainingProgramGraph : IGraph {
        public int Size { get; private set; }

        public int TotalTime { get; private set; }
        public List<double> gearArr = new List<double>();
        public List<double> timeArr = new List<double>();
        public List<KeyValuePair<double, double>> myValueList;
        public ColumnSeries columnSerie;


        public TrainingProgramGraph(ColumnSeries columnSerie) {
            Console.WriteLine("TrainingProgramGraph init...");
            this.columnSerie = columnSerie;
            Draw();
        }


        public void LoadTrainingProgram() {
            // Configure open file dialog box
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.Title = "Choose a Training Program";
//            oFD.InitialDirectory = @"Desktop";

            oFD.FileName = "beginner"; // Default file name
            oFD.DefaultExt = ".mj"; // Default file extension
            oFD.Filter = "Text documents (.mj)|*.mj"; // Filter files by extension // Show open file dialog box

            // Did they click on the OK button?
            if (oFD.ShowDialog() == true) {
                try {
                    using (StreamReader sr = File.OpenText(oFD.FileName)) {
                        string input = null;
                        int i = 0;
                        bool gearType = true;
                        int temp;
                        int tempGear = -1;
                        int tempTime = -1;
                        while ((input = sr.ReadLine()) != null) {
                            temp = Convert.ToInt32(input);

                            if (gearType) {
                                gearArr.Add(temp);
                                tempGear = temp;
                                gearType = false;
                            } else {
                                timeArr.Add(temp);
                                TotalTime += temp;
                                tempTime = temp;
                                gearType = true;
                            }

                            //myValueList.Add(new KeyValuePair<double, double>(tempTime, tempGear));
                            i++;
                        }
                    }
                } catch (Exception ex) {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
            Draw();
        }

        public void Draw() {
            double[] timeArr2 = timeArr.ToArray();
            double[] geatArr2 = gearArr.ToArray();

            myValueList = timeArr2.Select((t, i) => new KeyValuePair<double, double>(t, gearArr[i])).ToList();

            //Setting data for column chart
            columnSerie.DataContext = myValueList;
        }

        public void OnUpdate() {
            Draw();
        }
    }
}